﻿ using MahApps.Metro.Controls;
using ReflectionStudio.Classes;
using ReflectionStudio.Core.Events;
using System;
using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    //public partial class MainWindow : RibbonWindow, IIntexReptServBLCallback
    public partial class MasterWindow
    {
        #region ==ActiveDoc===========
        public static readonly DependencyProperty ActiveDocProperty = DependencyProperty.Register("ActiveDoc",
            typeof(string), typeof(MasterWindow),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnActiveDocChanged));
        public string ActiveDoc
        {
            get
            {
                return (string)GetValue(ActiveDocProperty);
            }
            set
            {
                SetValue(ActiveDocProperty, value);
                OnActiveDocChg();
            }
        }
        private static void OnActiveDocChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as MasterWindow;
            if (src != null)
            {
                src.ActiveDoc = (string)e.NewValue;
            }
        }
        public virtual void OnActiveDocChg()
        {
            if (vm == null)
                return;
            vm.ActiveDoc = ActiveDoc;
        }
        #endregion
        public string LayoutFile
        {
            get { return app.LayoutFile; }
        }
        #region ==IsLogOn===========
        public static readonly DependencyProperty IsLogOnProperty = DependencyProperty.Register("IsLogOn",
            typeof(bool), typeof(MasterWindow),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsLogOnChanged));
        public bool IsLogOn
        {
            get
            {
                return (bool)GetValue(IsLogOnProperty);
            }
            set
            {
                SetValue(IsLogOnProperty, value);
                if (value)
                {
                    CUSER = app.User;
                    DisplayHomeDocument();
                }
                vm.IsLogOn = value;
            }
        }
        private static void OnIsLogOnChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as MasterWindow;
            if (src != null)
            {
                src.IsLogOn = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsHelp===========
        public static readonly DependencyProperty IsHelpProperty = DependencyProperty.Register("IsHelp",
            typeof(bool), typeof(MasterWindow),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsHelpChanged));
        public bool IsHelp
        {
            get
            {
                return (bool)GetValue(IsHelpProperty);
            }
            set
            {
                SetValue(IsHelpProperty, value);
                app.IsHelp = value;
            }
        }
        private static void OnIsHelpChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as MasterWindow;
            if (src != null)
            {
                src.IsHelp = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==IsSingleRun===========
        public static readonly DependencyProperty IsSingleRunProperty = DependencyProperty.Register("IsSingleRun",
            typeof(bool), typeof(MasterWindow),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsSingleRunChanged));
        public bool IsSingleRun
        {
            get
            {
                return (bool)GetValue(IsSingleRunProperty);
            }
            set
            {
                SetValue(IsSingleRunProperty, value);
            }
        }
        private static void OnIsSingleRunChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as MasterWindow;
            if (src != null)
            {
                src.IsSingleRun = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsMenuTree===========
        public static readonly DependencyProperty IsMenuTreeProperty = DependencyProperty.Register("IsMenuTree",
            typeof(bool), typeof(MasterWindow),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsMenuTreeChanged));
        public bool IsMenuTree
        {
            get
            {
                return (bool)GetValue(IsMenuTreeProperty);
            }
            set
            {
                SetValue(IsMenuTreeProperty, value);
            }
        }
        private static void OnIsMenuTreeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as MasterWindow;
            if (src != null)
            {
                src.IsMenuTree = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsWFMSG===========
        public static readonly DependencyProperty IsWFMSGProperty = DependencyProperty.Register("IsWFMSG",
            typeof(bool), typeof(MasterWindow),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsWFMSGChanged));
        public bool IsWFMSG
        {
            get
            {
                return (bool)GetValue(IsWFMSGProperty);
            }
            set
            {
                SetValue(IsWFMSGProperty, value);
            }
        }
        private static void OnIsWFMSGChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as MasterWindow;
            if (src != null)
            {
                src.IsWFMSG = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==CUSER===========
        public static readonly DependencyProperty CUSERProperty = DependencyProperty.Register("CUSER",
            typeof(string), typeof(MasterWindow),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCUSERChanged));
        public string CUSER
        {
            get
            {
                return (string)GetValue(CUSERProperty);
            }
            set
            {
                SetValue(CUSERProperty, value);
            }
        }
        private static void OnCUSERChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as MasterWindow;
            if (src != null)
            {
                src.CUSER = (string)e.NewValue;
            }
        }
        #endregion

        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        string AgentID { get { return app.proxy.AgentID; } }
        public string RUNID = "";
        MetroViewModel vm;
        VICC_MENUDEF _VMU;
        VICC_MENUDEF VMU
        {
            get
            {
                if (_VMU == null) _VMU = new VICC_MENUDEF();
                return _VMU;
            }
        }
        private Process _process;
        UIMENU menu
        {
            get
            {
                return (UIMENU)this.FindName("MainMenu");
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public MasterWindow()
        {
            try
            {
                if (DesignerProperties.GetIsInDesignMode(this))
                    return;
                app.Load();

                IsSingleRun = app.IsSingleRun;
                IsMenuTree = app.IsMenuTree;
                app.IsSTA = true;
                app.PrepareMulLang();
                if (!app.Connected)
                {
                    throw new Exception("请检查网络或重试");
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            try
            {
                RUNID = "ROOT";
                vm = new MetroViewModel();
                this.DataContext = vm;
                vm.IntexAccount = (app.reportusers + app.itxonline) + "/" + app.license;
                InitializeComponent();
                IsLogOn = false;
                if (!app.Connected)
                {
                    //update hrstamm set HRDTELD=18,HRFILLER='20160830' where  HRBENID ='INTEX   '
                    throw new Exception("因为授权或网络问题，登录失败,:" + app.INFO);
                }
                Tracer.Verbose("主程序启动", "开始");

                DocumentFactory.Instance.MainContent = MainContent;
                DocumentFactory.Instance.IsSingleRun = IsSingleRun;
                DocumentFactory.Instance.Initialize(MainTab);
                VMU.LoadAssembly(Assembly.GetExecutingAssembly());
                VMU.LoadAssembly("NLog");
                VMU.LoadAssembly("comhlp");
                VMU.LoadAssembly("ConsoleClient");
                 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        private void DisplayHomeDocument()
        {
            string doctype = dicParamater["DocType"].ToString();
            menu.ActivateReportByDocType(doctype);
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            app.Quit();
            base.OnClosing(e);
            if (_process != null)
            {
                _process.Refresh();
                _process.Close();
            }
        }

        private void SetMasterCommand(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            ImageBrush brush = btn.Background as ImageBrush;
            MessageInfo msg = new MessageInfo();
            if (app.IsMaster)
            {
                app.IsMaster = false;
                brush.RelativeTransform = null;
            }
            else
            {
                app.IsMaster = true;
                Helpers.Helper.RotateImage(btn);
            }
            msg.Sender = this.RUNID;
            msg.MSGTYPE = "CHAGELVL";
            msg.ACTION = "IsMaster=true";
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

        private void SetLogCommand(object sender, RoutedEventArgs e)
        {
            vm.IsShowLog = !vm.IsShowLog;
        }
        private void SetLogOnCommand(object sender, RoutedEventArgs e)
        {
            IsLogOn = false;

        }
        private void CloseWindowCommand(object sender, RoutedEventArgs e)
        {
            this.Close();
            System.Environment.Exit(0);
        }

        private void DBQueryCommand(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.DB02(this.RUNID);
        }
        private void TSModeCommand(object sender, RoutedEventArgs e)
        {

            vm.IsTS = false;
            app.AppMode = "TMODE";
            //app.Mandant = "01";
            MessageInfo msg = new MessageInfo();
            msg.Sender = this.RUNID;
            msg.MSGTYPE = "CHGMODE";
            msg.ACTION = "CHGMODE";
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

        private void TMModeCommand(object sender, RoutedEventArgs e)
        {
            app.AppMode = "TISSUE";
            vm.IsTS = true;
            //app.Mandant = "00";
            MessageInfo msg = new MessageInfo();
            msg.Sender = this.RUNID;
            msg.MSGTYPE = "CHGMODE";
            msg.ACTION = "CHGMODE";
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

        private void CNModeCommand(object sender, RoutedEventArgs e)
        {
            app.LANG = "V";
            vm.IsCN = true;
            vm.IsEN = false;
            MessageInfo msg = new MessageInfo();
            msg.Sender = this.RUNID;
            msg.MSGTYPE = "CHGLANG";
            msg.ACTION = "CHGLANG";
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }
        private void ZMModeCommand(object sender, RoutedEventArgs e)
        {
            vm.IsZM = !vm.IsZM;
            MessageInfo msg = new MessageInfo();
            msg.Sender = this.RUNID;
            msg.MSGTYPE = "CHGZM";
            msg.ACTION = "CHGZM";
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }
        private void ENModeCommand(object sender, RoutedEventArgs e)
        {
            app.LANG = "E";
            vm.IsEN = true;
            vm.IsCN = false;
            MessageInfo msg = new MessageInfo();
            msg.Sender = this.RUNID;
            msg.MSGTYPE = "CHGLANG";
            msg.ACTION = "CHGLANG";
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

        private void ITXSetUp(object sender, RoutedEventArgs e)
        {
            MessageInfo msg = new MessageInfo();
            msg.Sender = RUNID;
            msg.MSGTYPE = "OPENEXE";
            msg.ACTION = "OPENEXE";
            msg.Where = "WORKFLOW";//important
            msg.What = "ITXSETUP";
            VICC_WFEVENTS.SVWF.CheckEvents("OPENEXE", null, "WORKFLOW", msg);
        }

        private void SDKEYSetUp(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.SD02(RUNID, "0001");
        }

        private void HelpCommand(object sender, RoutedEventArgs e)
        {
            if (!IsHelp)
            {
                cHelp.EXAUF = ((VICC_MENUDEF)MainMenu.MV).ActiveDoc;
                IsHelp = true;
                cHelp.Width = 400;
            }
            else
            {
                if (cHelp.Width >= 1000)
                    IsHelp = false;
                cHelp.Width = cHelp.Width * 2;
            }
        }
        private void ToggleFlyout(int index)
        {
            var flyout = this.Flyouts.Items[index] as Flyout;
            if (flyout == null)
            {
                return;
            }

            flyout.IsOpen = !flyout.IsOpen;
        }
        private void WFModeCommand(object sender, RoutedEventArgs e)
        {
            IsWFMSG = !IsWFMSG;
            this.ToggleFlyout(0);
        }
    }
}
